package net.qhzw.vip.qiyu.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.dto.DispatchHistoryDto;
import net.qhzw.vip.qiyu.dto.TimelineDto;
import net.qhzw.vip.qiyu.entity.MaintRpt;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.MaintRptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/maint/admin")
@Api(tags = "维修报案后台接口")
public class MaintAdminController {


    @Autowired
    private MaintRptService maintRptService;


    @ApiOperation(value = "查看派单历史")
    @GetMapping("dispatchHistory/{id}")
    public JsonResult findDispatchHistory(@PathVariable Long id) {
        List<DispatchHistoryDto> dispatchHistoryDtos = maintRptService.dispatchHistory(id);
        return JsonResult.ok(dispatchHistoryDtos);
    }


    @ApiOperation(value = "查看时间线")
    @GetMapping("timeline/{id}")
    public JsonResult findTimeline(@PathVariable Long id) {
        MaintRpt maintRpt = maintRptService.getById(id);
        List<TimelineDto> timelineDtos = null;
        if (!ObjectUtils.isEmpty(maintRpt)) {
            timelineDtos = new ArrayList<>(5);
            TimelineDto timelineDto1 = new TimelineDto("提交申请", dateFormat(maintRpt.getCreated()));
            TimelineDto timelineDto2 = new TimelineDto("系统派单", dateFormat(maintRpt.getDispatchTime()));
            TimelineDto timelineDto3 = new TimelineDto("维修师傅接单", dateFormat(maintRpt.getOrderTakingTime()));
            TimelineDto timelineDto4 = new TimelineDto("到达现场", dateFormat(maintRpt.getArriveTime()));
            TimelineDto timelineDto5 = new TimelineDto("维修完成", dateFormat(maintRpt.getMaintTime()));
            timelineDtos.add(timelineDto1);
            timelineDtos.add(timelineDto2);
            timelineDtos.add(timelineDto3);
            timelineDtos.add(timelineDto4);
            timelineDtos.add(timelineDto5);
        }
        return JsonResult.ok(timelineDtos);
    }

    private String dateFormat(Date date) {
        if (ObjectUtils.isEmpty(date)) {
            date = new Date();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }


}
